import { type CrudConfig, type FormFieldConfig } from '@/components';
import urlConstants from '@/constants/urlConstants';
import type { ProColumns } from '@ant-design/pro-components';
import { Tag } from 'antd';

// 岗位数据类型
export interface PositionRecord {
  positionId: string;
  positionName: string;
  parentId?: string;
  parentName?: string;
  description: string;
  status: 'active' | 'inactive';
  createTime?: string;
  updateTime?: string;
}

// 统一的页面配置函数 - 接收 translate 函数作为参数
export const getPageConfig = (translate: (prefix: string, key: string) => string) => {
  const t = (key: string) => translate('systemManage', key);

  return {
    // CRUD配置
    crudConfig: {
      endpoints: {
        list: urlConstants.POSITION_MANAGEMENT.LIST,
        create: urlConstants.POSITION_MANAGEMENT.CREATE,
        update: urlConstants.POSITION_MANAGEMENT.EDIT,
        delete: urlConstants.POSITION_MANAGEMENT.DELETE,
        detail: urlConstants.POSITION_MANAGEMENT.LIST,
      },
    } as CrudConfig,

    // 搜索字段配置
    searchFields: [
      {
        name: 'positionId',
        label: t('positionId'),
        type: 'input' as const,
      },
      {
        name: 'positionName',
        label: t('positionName'),
        type: 'input' as const,
      },
    ],

    // 表格列配置
    columns: [
      {
        title: t('serialNumber'),
        dataIndex: 'paramIndex',
        key: 'paramIndex',
        width: 80,
        fixed: 'left',
      },
      {
        title: t('positionId'),
        dataIndex: 'positionId',
        key: 'positionId',
        width: 120,
        copyable: true,
      },
      {
        title: t('positionName'),
        dataIndex: 'positionName',
        key: 'positionName',
        width: 180,
        ellipsis: true,
      },
      {
        title: t('parentName'),
        dataIndex: 'parentName',
        key: 'parentName',
        width: 150,
        ellipsis: true,
        render: (text: string) => text || <span style={{ color: '#999' }}>{t('topPosition')}</span>,
      },
      {
        title: t('status'),
        dataIndex: 'status',
        key: 'status',
        width: 100,
        render: (text: string) => {
          const statusMap = {
            active: { color: 'green', text: t('statusActive') },
            inactive: { color: 'red', text: t('statusInactive') },
          };
          const status = statusMap[text as keyof typeof statusMap] || { color: 'default', text: text };
          return <Tag color={status.color}>{status.text}</Tag>;
        },
      },
      {
        title: t('description'),
        dataIndex: 'description',
        key: 'description',
        width: 250,
        ellipsis: true,
        render: (text: string) => <div style={{ wordWrap: 'break-word', whiteSpace: 'normal' }}>{text}</div>,
      },
    ] as ProColumns<PositionRecord>[],

    // 表单字段配置
    formFields: [
      {
        name: 'positionName',
        label: t('positionName'),
        type: 'input' as const,
        rules: [{ required: true, message: t('positionNameRequired') }],
      },
      {
        name: 'parentId',
        label: t('parentName'),
        type: 'select' as const,
        options: [], // 这里应该从API获取岗位树数据
      },
      {
        name: 'status',
        label: t('status'),
        type: 'select' as const,
        options: [
          { label: t('statusActive'), value: 'active' },
          { label: t('statusInactive'), value: 'inactive' },
        ],
        rules: [{ required: true, message: t('statusRequired') }],
      },
      {
        name: 'description',
        label: t('description'),
        type: 'textarea' as const,
        span: 24,
      },
    ] as FormFieldConfig[],

    // 字典枚举配置
    dictEnum: {},
  };
};
